/**
 * 
 */

$(document).ready(function()
  {
      var i = 0;
  
      $('#images img').each(function(){
      var me = this;
      $(this).hide();
      var j = i;
      setTimeout(function(){
        $(me).fadeIn(1000);
      }, i);
      i += 100;

      $( "#register_dob" ).datepicker({
			changeMonth: true,
			changeYear: true
		});
   });


   $(function() {
  		$( "#register_dob" ).datepicker({
  			changeMonth: true,
  			changeYear: true
  		});
  	});
   
   var value = '{TAB_ID}';
   if (value == 'signuplink')
   {
       $("#loginlink").removeClass('select');
       $("#signuplink").addClass('select');
       $("#loginbox").slideUp();
       $("#regbox").slideDown();
   }
   else
   {
       $("#signuplink").removeClass('select');
       $("#loginlink").addClass('select');
       $("#regbox").slideUp();
       $("#loginbox").slideDown();
   }
  
  $(".tab").click(function()
  {
      var X=$(this).attr('id');
      
      if(X=='signuplink')
      {
          $("#loginlink").removeClass('select');
          $("#signuplink").addClass('select');
          $("#loginbox").slideUp();
          $("#regbox").slideDown();
      }
      else
      {
          $("#signuplink").removeClass('select');
          $("#loginlink").addClass('select');
          $("#regbox").slideUp();
          $("#loginbox").slideDown();
      }
  
  });

//----------------------------------------------------------
// Field validation

//	email

function validateEmail()
{
	var email = $("#register_email").val();
	var msgbox = $("#emailstatus");


	$("#emailstatus").html('<img src="resources/images/small/loader.gif" align="absmiddle">&nbsp;Checking availability...');

	$.ajax({
    	type: "POST",
      	url: "authenticate/check/email",
      	data: "register_email="+ email,
      	success: function(msg){
     
		$("#emailstatus").ajaxComplete(function(event, request, settings){
  
		var d = msg;
		var str = msg.substr(0, 2);

		if(str == 'OK')
		{
			$("#register_email").removeClass("no");
			$("#register_email").addClass("yes");
			msgbox.html('<img src="resources/images/small/yes.png" align="absmiddle"> <font color="Green"> Available </font>  ');
		}
		else
		{
			$("#register_email").removeClass("yes");
			$("#register_email").addClass("no");
			msgbox.html(email+" is already in use. ");

			return false;
		}
  
		});

		}
  
	});
  
    return true;
 }
      
	$.validator.addMethod("email", function(value, element) {
    var valid = this.optional(element) || /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/i.test(value);

    if (valid)
    {
    	valid = validateEmail();  
    }
    else
    {
    	$("#register_email").removeClass("yes");
		$("#register_email").addClass("no");

		var msgbox = $("#emailstatus");
		msgbox.html("");
    }

    return valid;
    
    }, "Give valid email address.");
  
	$.validator.addMethod("username",function(value,element){
		var username = $("#register_name").val();
		var valid = this.optional(element) || username.length > 3;
		if (valid)
		{
			$("#register_name").removeClass("no");
			$("#register_name").addClass("yes");
		}
		else
		{
			$("#register_name").removeClass("yes");
			$("#register_name").addClass("no");
		}

		return valid;
	},"Invalid username");
    
	$.validator.addMethod("password",function(value,element){
		var valid = this.optional(element) || /^[A-Za-z0-9!@#$%^&*()_]{6,16}$/i.test(value);

		if (valid)
		{
			$("#register_password").removeClass("no");
			$("#register_password").addClass("yes");	
		}
		else
		{
			$("#register_password").removeClass("yes");
			$("#register_password").addClass("no");
		}	

		return valid;
		
	},"Passwords are 6-16 characters");

	$.validator.addMethod("confimpwd",function(value,element){
		var password = $("#register_password").val();
		var compPwd = $("#register_password_confirm").val();
		
		var valid =  this.optional(element) || (password==compPwd);
		if (valid)
		{
			$("#register_password_confirm").removeClass("no");
			$("#register_password_confirm").addClass("yes");
		}
		else
		{
			$("#register_password_confirm").removeClass("yes");
			$("#register_password_confirm").addClass("no");
		}

		return valid;
		
		},"Password doesnt match");


	//Validate signup form
	$("#signup").validate({
	rules: {
  		register_email: "required email",
  		register_password: "required password",
  		register_password_confirm: "required confimpwd",
  		register_name: "username"
      		} 
		});  
});
  	  